<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      v-on="$listeners"
      @close="onClose"
      title="订单管理"
    >
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="83px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品图片">
              <el-col :push="2" :span="4" v-for="item in flowerInfor.imgUrlList" >
                <el-image  style="width: 100px;height: 100px ;margin-bottom: 8px" :src="item"></el-image>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品标题">
              <el-input
                      v-model="flowerInfor.title"
                      :disabled="true"
                      clearable
                      :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户" prop="nickName">
              <el-input
                v-model="formData.nickName"
                placeholder="请输入用户"
                :disabled="true"
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="iphone">
              <el-input
                v-model="formData.iphone"
                placeholder="请输入电话"
                :disabled="true"
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收货人" prop="userName">
              <el-input
                v-model="formData.userName"
                :disabled="flag >= 1"
                placeholder="请输入收货人"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货电话" prop="userIphone">
              <el-input
                :disabled="flag >= 1"
                v-model="formData.userIphone"
                placeholder="请输入收货电话"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收货地址" prop="address">
              <el-input
                :disabled="flag >= 1"
                v-model="formData.address"
                placeholder="请输入收货地址"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="addressInfo">
              <el-input
                :disabled="flag >= 1"
                v-model="formData.addressInfo"
                placeholder="请输入详细地址"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="送达时间" prop="distributeTime">
              <el-input
                v-model="formData.distributeTime"
                :disabled="flag >= 1"
                placeholder="请输入送达时间"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="state">
              <el-select
                v-model="formData.state"
                placeholder="请选择状态"
                clearable
                :disabled="flag >= 1"
                :style="{ width: '100%' }"
              >
                <el-option
                  v-for="(item, index) in options"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="祝福" prop="blessing">
          <el-input
            :disabled="flag >= 1"
            v-model="formData.blessing"
            type="textarea"
            placeholder="请输入祝福"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import ordersApi from "../../api/ordersApi";
import flowerApi from "../../api/flowerApi";

export default {
  inheritAttrs: false,
  components: {},
  props: [],
  data() {
    return {
      flag: 0,
      formData: {
        nickName: undefined,
        iphone: undefined,
        userName: undefined,
        userIphone: undefined,
        address: undefined,
        addressInfo: undefined,
        distributeTime: undefined,
        state: 0,
        blessing: undefined,
      },
      rules: {
        nickName: [],
        iphone: [],
        userName: [
          {
            required: true,
            message: "请输入收货人",
            trigger: "blur",
          },
        ],
        userIphone: [
          {
            required: true,
            message: "请输入收货电话",
            trigger: "blur",
          },
        ],
        address: [
          {
            required: true,
            message: "请输入收货地址",
            trigger: "blur",
          },
        ],
        addressInfo: [
          {
            required: true,
            message: "请输入详细地址",
            trigger: "blur",
          },
        ],
        state: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change",
          },
        ],
      },
      /*   * 0未确认,1已发货,2确认收货,3已经评价,4交易完成,5卖家取消,6买家取消*/
      stateOptions: [
        {
          label: "未确认",
          value: 0,
        },
        {
          label: "已发货待确认",
          value: 1,
        },
        {
          label: "已收货等待评价",
          value: 2,
        },
        {
          label: "已评价交易完成",
          value: 3,
        }, {
          label: "卖家取消 ",
          value: 4,
        }, {
          label: "买家取消 ",
          value: 5,
        }
      ],
      flowerInfor:{}
    };
  },
  computed: {
    options() {
      return this.stateOptions.filter((item) => {
        return item.value == this.flag + 1 || item.value == this.flag;
      });
    },
  },
  methods: {
    open(item) {
      flowerApi.get(item.flowerId).then(res=>{
        this.flowerInfor=res.data
      })
      this.formData = item;
      this.flag = item.state;
      this.$emit("update:visible", true);
    },
    onClose() {
      this.$refs["elForm"].resetFields();
    },
    close() {
      this.$emit("update:visible", false);
    },
    handelConfirm() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          delete this.formData.createDate;
          ordersApi.update(this.formData).then((res) => {
            this.$emit("query");
            if (this.flag != this.formData.state) {
              let msg = {
                message: this.formData.state,
                toUserId: this.formData.userId,
                type:"order"
              };
              this.$store.state.websocket.send(msg)
            }
            this.close();
          });
        }
      });
    },
  },
};
</script>
<style>
</style>
